<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Core_model extends CI_Model{
	function getalldata($table="", $order="", $lim="") {
		$limit = "";
		if ($lim != "") {
			$limit = "limit ".$lim[0].", ".$lim[1]." ";
		}  
		if ($order != "" ) {
			if ($order == "desc") {
				$order = " ORDER BY stateid DESC ";
			} else if ($order == "rand") {
				$order = " ORDER BY RAND() ";
			} else if ($order == "count") {
				$order = " ORDER BY viewcount DESC ";
			} else {
				$order = " ORDER BY stateid ASC ";
			}
		}
		$sql = "SELECT * FROM ".$table." ".$order." ".$limit;
		return $this->db->query($sql)->result_array();
	}
	
	function countalldata($table="", $where="") {
		if($where != "") {
			$this->db->where($where);
		}
		$count = $this->db->from($table)->count_all_results();
		return $count;		
		//return $this->db->count_all($table);
	}
	
	function getwheredata($table="", $where="", $order="", $limit="") {
		if ( $limit!="" ) {
			$this->db->order_by($order);
			$this->db->limit($limit[1], $limit[0]);
		}
		$query = $this->db->get_where($table, $where);
		return $query->result_array();
	}
	
	function getranddata($table="", $where="", $limit) {
		$sql = "SELECT * FROM ".$table." 
				WHERE stateid ".$where['stateid']."
				AND permalink ".$where['permalink']."
				ORDER BY RAND() LIMIT ".$limit[0].", ".$limit[1];
		return $this->db->query($sql)->result_array();
	}
	
	function updatedata($table="", $view="", $where="") {
		$this->db->where($where);
		return $this->db->update($table, $view); 
	}
}